package com.view.houseon;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.presentacion.houseon.R;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;



public class Visualizar extends Activity implements OnCancelListener  {
	private MjpegView mv;
	private MjpegView mv2;
	private MjpegView mv3;
	private MjpegView mv4;
	Thread			thread;
	Handler 		handler;
	boolean 		cancel;
	int cptfichier =0;
	TextView txt;
	LinearLayout linear; 
	private double  time_unique=0;
	public static  Bitmap test;	
	
	
     OutputStream fOut = null;
     
     
	public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
                //sample public cam
       //String URL = "http://shibuya.ipcam.jp:60001/nphMotionJpeg?Resolution=320x240&Quality=Standard"; 
       
       String URL = "http://192.168.1.33:11341/cam_1.cgi"; 
      /*  String URL2 = "http://192.168.1.39:11341/cam_1.cgi"; 
        String URL3 = "http://192.168.1.39:11341/cam_1.cgi"; 
        String URL4 = "http://192.168.1.39:11341/cam_1.cgi"; 
      */  
        final String path = Environment.getExternalStorageDirectory().toString();
        // shibuya.ipcam.jp :: Adresse IP 114.145.160.176 port : 60001
        Toast.makeText(getBaseContext(), URL, 1000).show();
      
        
       //quitar titulo de ventana 
     requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        
   
        
    /*//////////////////////////////////////////////////    
        
     linear = new LinearLayout(this);
//
       //linear.setOrientation(LinearLayout.VERTICAL);

        final ImageView  img=new ImageView (this);
           txt=new TextView (this);
        Button   btn=new Button (this);
        btn.setText("capturar imagen");
        linear.addView(btn);
        linear.addView(img);
        linear.addView(txt);
       
        
        linear.setOrientation(LinearLayout.VERTICAL);
        linear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
      
     
 btn.setOnClickListener(new View.OnClickListener() {
        	
            public void onClick(View v) {
            	img.setImageBitmap(test);
            	txt.setText("Image "+path+ cptfichier++);
            try {
            	File file = new File(path,cptfichier+".jpg");
            	fOut = new FileOutputStream(file);

            	test.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            	
					fOut.flush();
					fOut.close();
				} catch (IOException e) {
					
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	

            //	test.compress("png', quality, stream)
             
            }
        });

 *///////////////////////////////////////   		
 			 
		 setContentView(R.layout.activity_video); 
		 
		    mv = new MjpegView(this); 
	        
	        LinearLayout midLL = (LinearLayout)findViewById(R.id.Surface1);
	     //   LinearLayout midLL = new LinearLayout(this); 
	        midLL.findViewById(R.id.Surface1); 
	        midLL.addView(mv);
	       // midLL.addView(new MjpegView(this));
	     
	        mv.setSource(MjpegInputStream.read(URL));
	        mv.setDisplayMode(MjpegView.TAM_OK);
	        mv.showFps(true);
	       
	    /*    
	
	        mv2 = new MjpegView(this); 
	        LinearLayout midLL2 = (LinearLayout)findViewById(R.id.Surface2);
	     //   LinearLayout midLL = new LinearLayout(this); 
	        midLL2.findViewById(R.id.Surface2); 
	        midLL2.addView(mv2);
	        //midLL2.addView(new MjpegView(this));
	     
	        mv2.setSource(MjpegInputStream.read(URL2));
	        mv2.setDisplayMode(MjpegView.TAM_OK);
	        mv2.showFps(true);
	 
	       

	        mv3 = new MjpegView(this); 
	        LinearLayout midLL3 = (LinearLayout)findViewById(R.id.Surface3);
	     //   LinearLayout midLL = new LinearLayout(this); 
	        midLL3.findViewById(R.id.Surface3); 
	        midLL3.addView(mv3);
	        //midLL2.addView(new MjpegView(this));
	     
	        mv3.setSource(MjpegInputStream.read(URL3));
	        mv3.setDisplayMode(MjpegView.TAM_OK);
	        mv3.showFps(true);
	        

	        mv4 = new MjpegView(this); 
	        LinearLayout midLL4 = (LinearLayout)findViewById(R.id.Surface4);
	     //   LinearLayout midLL = new LinearLayout(this); 
	        midLL4.findViewById(R.id.Surface4); 
	        midLL4.addView(mv4);
	        //midLL2.addView(new MjpegView(this));
	     
	        mv4.setSource(MjpegInputStream.read(URL4));
	        mv4.setDisplayMode(MjpegView.TAM_OK);
	        mv4.showFps(true);
	        
		 */

        
        
		
        
        //	} 
        
        
        
         
        
	}
	

	
	public void onPause() {
		super.onPause();
		mv.stopPlayback();
		
	}
	public void onCancel(DialogInterface arg0) {
		cancel=true;	
		super.onPause();
		mv.stopPlayback();
		
		
	}
	
	
	
}


